<template>
  <div class="twt-form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form
      :model="formData"
      :label-width="labelWidth"
      :label-position="labelPosition"
      :size="sizeForm"
    >
      <el-row>
        <template v-for="(item, index) in formItems" :key="item.label">
          <el-col v-bind="colLayout">
            <el-form-item :label="item.label" :style="itemStyle">
              <template v-if="item.type === 'input'">
                <el-input
                  type="text"
                  v-bind="item.otherOptions"
                  :placeholder="item.placeholder"
                  v-model="formData[`${item.field}`]"
                ></el-input>
              </template>
              <template v-if="item.type === 'select'">
                <el-select
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  :placeholder="item.placeholder"
                  v-model="formData[`${item.field}`]"
                >
                  <el-option
                    v-for="option in item.optionValue"
                    :key="option.value"
                    :value="option.value"
                    :label="option.title"
                  ></el-option>
                </el-select>
              </template>
              <template v-if="item.type === 'datepicker'">
                <el-date-picker
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  v-model="formData[`${item.field}`]"
                ></el-date-picker>
              </template>
              <template v-if="item.type === 'radio'">
                <el-radio-group v-model="formData[`${item.field}`]">
                  <el-radio
                    v-for="radioValue in item.optionValue"
                    :key="radioValue.value"
                    :label="radioValue.value"
                    >{{ radioValue.title }}</el-radio
                  >
                </el-radio-group>
              </template>
            </el-form-item>
            <template v-if="item.type === 'other'">
              <slot :name="item.slotName"></slot>
            </template>
          </el-col>
          <slot name="searchBtn" v-if="index === 3"></slot>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, watch } from 'vue'
import { IFormItem } from '@/base-ui/form/type'

export default defineComponent({
  props: {
    modelValue: {
      type: Object,
      required: true
    },
    formItems: {
      type: Array as PropType<IFormItem[]>,
      required: true
    },
    labelWidth: {
      type: String,
      default: '73px'
    },
    labelPosition: {
      type: String,
      default: 'right'
    },
    itemStyle: {
      type: Object,
      default: () => ({})
    },
    colLayout: {
      type: Object,
      default: () => ({
        xl: 5, // >1920px 4个
        lg: 5,
        md: 12,
        sm: 24,
        xs: 24
      })
    },
    sizeForm: {
      type: String,
      default: 'small'
    }
  },
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    const formData = ref({ ...props.modelValue })
    watch(
      formData,
      (newValue) => {
        emit('update:modelValue', newValue)
      },
      {
        deep: true
      }
    )
    return {
      formData
    }
  }
})
</script>

<style scoped lang="less">
/deep/.el-radio {
  margin-right: 2px;
}
/deep/.el-radio__label {
  padding-left: 8px;
  font-size: 8px;
}
</style>
